package com.cpfmat.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cpfmat.common.CommonStringUtils;
import com.cpfmat.entity.RzTab;
import com.cpfmat.mapper.RzTabMapper;
import com.cpfmat.service.IRzTabService;
import com.cpfmat.vo.CustomRzTab;

@Service
public class RzTabServiceImpl extends ServiceImpl<RzTabMapper, RzTab> implements IRzTabService {
	@Autowired
	private RzTabMapper rzTabMapper;

	@Override
	public boolean log(String userId, String operation, String ip,String manageUser) {
		RzTab tab = new RzTab();
		tab.setSqlId(CommonStringUtils.getUUID());
		tab.setCzsj(new Date());
		tab.setUserId(userId);
		tab.setCzDis(operation);
		tab.setIp(ip);
		tab.setManageUser(manageUser);
		insert(tab);
		return false;
	}

	@Override
	public Page<CustomRzTab> selectLogList(String userName, String startDate, String endDate, String operation, String ip,
			int pageNum, int pageSize) {
		if(pageNum <= 0) {
			pageNum = 1;
		}
		
		Page<CustomRzTab> page = new Page<CustomRzTab>(pageNum, pageSize);

		Map<String, Object> param = new HashMap<String, Object>();

		if (StringUtils.isNotBlank(userName)) {
			param.put("userName", userName);
		}

		if (StringUtils.isNotBlank(startDate)) {
			param.put("startTime", startDate);
		}

		if (StringUtils.isNotBlank(endDate)) {
			param.put("endTime", endDate);
		}

		if (StringUtils.isNotBlank(operation)) {
			param.put("operation", operation);
		}

		if (StringUtils.isNotBlank(ip)) {
			param.put("ip", ip);
		}
		
		List<CustomRzTab> logList = rzTabMapper.selectLogList(page, param);
		
		page.setRecords(logList);

		return page;
	}

}
